package me.spring.cloud.common.components.mds.config;

import me.spring.cloud.common.components.mds.constants.DataSourceContants;
import me.spring.cloud.common.components.mds.dynamic.DynamicDataSource;
import me.spring.cloud.common.components.mds.util.DataSourceSet;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 配置多数据源
 */
@Configuration
public class DynamicDataSourceConfig {

  @Bean("dataSource")
  @Primary
  public DynamicDataSource dataSource(DataSource dataSource) {
    DataSourceSet.putTargetDataSourcesMap(DataSourceContants.MAINDATASOURCE, dataSource);
    return new DynamicDataSource(dataSource, DataSourceSet.getTargetDataSourcesMap());
  }

}